package routing;

import geocoding.CustomNodes;
import osm.OsmReader;
import android.location.Location;

public class Smooth {
	
	String TAG="URNav";
	
	
	public void smoothTripLandmark (Trip myTrip, CustomNodes cn){
		
		for (int i=0; i < myTrip.getTripList().size()-2; i++){
			if ((getAngle(myTrip.getTripItem(i), myTrip.getTripItem(i+1), myTrip.getTripItem(i+2),cn) < 25)  &&  
					(getAngle(myTrip.getTripItem(i), myTrip.getTripItem(i+1), myTrip.getTripItem(i+2),cn) > -25) && 
					(cn.getHas_tags(myTrip.getTripItem(i+1))==false))  
			
			{
				
				boolean steps=false; 
				for (int j=0; j<cn.getValueSize(i+1); j++) {
					if (cn.getValue(i+1, j).equals("steps")) {
						steps = true; 
					}
					
				}
				
				if (steps==false) {
				cn.setSilent(myTrip.getTripItem(i+1));
//				Log.i(TAG, "SetSilent: " + myOsmReader.getNodes().getOsmID(myTrip.getTripList().get(i+1)));
				}
			}
			
		}
		
		
	}
	
	public void smoothTripSimple (Trip myTrip, CustomNodes cn){
		
		for (int i=0; i < myTrip.getTripList().size()-2; i++){
			if ((getAngle(myTrip.getTripItem(i), myTrip.getTripItem(i+1), myTrip.getTripItem(i+2),cn) < 25)  &&  
					(getAngle(myTrip.getTripItem(i), myTrip.getTripItem(i+1), myTrip.getTripItem(i+2),cn) > -25))  
			
			{
				cn.setSilent(myTrip.getTripItem(i+1));
//				Log.i(TAG, "SetSilent: " + myOsmReader.getNodes().getOsmID(myTrip.getTripList().get(i+1)));
			}
			
		}
		
		
	}
	

	public float getAngle(int firstItem, int secondItem, int thirdItem, CustomNodes cn){
		
		Location firstLoc = new Location("x");
		Location secondLoc = new Location("x");
		Location thirdLoc = new Location("x");
		
		firstLoc.setLatitude(cn.getDlati(firstItem));
		firstLoc.setLongitude(cn.getDlongi(firstItem));
		
		secondLoc.setLatitude(cn.getDlati(secondItem));
		secondLoc.setLongitude(cn.getDlongi(secondItem));
		
		thirdLoc.setLatitude(cn.getDlati(thirdItem));
		thirdLoc.setLongitude(cn.getDlongi(thirdItem));

		float angle; 
		float bearing1;
		float bearing2;
		
//		Log.i(TAG, "1st: " + myOsmReader.getNodes().getDlati(firstItem) + " " + myOsmReader.getNodes().getDlongi(firstItem));
//		Log.i(TAG, "2nd: " + myOsmReader.getNodes().getDlati(secondItem) + " " + myOsmReader.getNodes().getDlongi(secondItem));
//		Log.i(TAG, "2rd: " + myOsmReader.getNodes().getDlati(thirdItem) + " " + myOsmReader.getNodes().getDlongi(thirdItem));
		
		
		bearing1 = firstLoc.bearingTo(secondLoc);
//		Log.i(TAG, "B1: " + bearing1);
		bearing2 = secondLoc.bearingTo(thirdLoc);
//		Log.i(TAG, "B2: " + bearing2);
		angle = bearing1 - bearing2;
		
		return angle;
		
	}
	
	
}
